package work.parabola.keyboardswitch.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import work.parabola.keyboardswitch.common.ResponseCode;
import work.parabola.keyboardswitch.common.RoleEnum;
import work.parabola.keyboardswitch.common.ServerResponse;
import work.parabola.keyboardswitch.common.UserConst;
import work.parabola.keyboardswitch.pojo.UserSession;
import work.parabola.keyboardswitch.pojo.query.manufacturer.AddManuQuery;
import work.parabola.keyboardswitch.pojo.query.manufacturer.ListQuery;
import work.parabola.keyboardswitch.service.IManufacturerService;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/manufacturer/")
public class ManufacturerController {

    @Autowired
    private IManufacturerService iManufacturerService;

    @RequestMapping("add.do")
    public ServerResponse add(HttpSession session, @RequestBody AddManuQuery addManuQuery) {
        UserSession userSession = (UserSession) session.getAttribute(UserConst.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        if (!userSession.getRoleType().equals(RoleEnum.MANAGER.getCode())) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.PRIVILEGE_ERROR.getCode(),
                    ResponseCode.PRIVILEGE_ERROR.getDesc());
        }
        return this.iManufacturerService.add(addManuQuery);
    }

    @PostMapping("getManufacturerList.do")
    public ServerResponse getManufacturerList(HttpSession session, @RequestBody ListQuery listQuery) {
        UserSession userSession = (UserSession) session.getAttribute(UserConst.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        if (!userSession.getRoleType().equals(RoleEnum.MANAGER.getCode())) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.PRIVILEGE_ERROR.getCode(),
                    ResponseCode.PRIVILEGE_ERROR.getDesc());
        }
        return this.iManufacturerService.getManufacturerList(listQuery);
    }
}
